
local japi = require 'jass.japi'
local buffs = {
    '假死复活',
    '技能蓄力',
    '击飞',
    '晕眩',
    '蓄力中',
    '被锁定',
}

function create_life1(w,h)
    local panel = class.panel:builder
    {
        x = -800,
        y = -200,
        w = w,
        h = h,
        show_flag = false,
        world_auto_remove = false,
        parent = ac.frame.portrait,
        normal_image = [[core\hpbar\backdrop-hero.tga]],
        world_anchor = 'top',
        buffer_rate = 1,
        updata_buff = function(self,unit)
            for _,name in ipairs(buffs) do
                local buff = unit:find_buff(name)
                if buff then
                    local title = format_obj(buff,buff.title)
                    if self.title.text~=title then
                        self.title:set_text(title)
                    end
                    if buff.time>0 then
                        self.state:set_width(self.max_w*buff:get_remaining()/buff.time)
                    else
                        self.state:set_width(self.max_w)
                    end
                    self.state:show()
                    return
                end
            end
            local title = unit.name
            if unit.owner:is_player() then
                title = unit.owner:get_world_name()
            end
            if self.title.text~=title then
                self.title:set_text(title)
            end
            self.state:hide()
        end,
        updata_shield = function(self,unit)
            local hpmax = unit:get '护盾上限'
            local shield1 = unit:get '护盾'
            if shield1>0 then
                local race1 = shield1/hpmax
                local rate2 = unit:get '生命'/unit:get('生命上限')
                if race1+rate2>1 then
                    self.shield:set_position(self.max_w*(1-race1)+0.01,0)
                else
                    self.shield:set_position(self.life.w,0)
                end
                self.shield:set_width(self.max_w*race1)
                self.shield:show()
            else
                self.shield:hide()
            end
        end,
        updata_cost = function(self,unit)
            local cost = unit:get '魔法'
            local mpmax = unit:get '魔法上限'
            local image = unit:get_data('使用能量')
            if image then
                cost = unit:get '能量'
                mpmax = unit:get '能量上限'
                self.cost:set_normal_image(image)
            else
                self.cost:set_normal_image([[core\hpbar\蓝条.tga]])
            end
            if mpmax>0 and cost>0 then
                self.cost:set_width(self.max_w*cost/mpmax)
                self.cost:show()
            else
                self.cost:hide()
            end
        end,
        update = function(self)
            local player = ac.player.self
            local unit = self.world_widget
            local life = unit:get '生命'
            local hpmax = unit:get '生命上限'
            local rate = life/hpmax
            
            local load1 = math.max(math.abs(rate - self.buffer_rate)*0.05,0.01)
            if rate>=self.buffer_rate then
                self.buffer_rate = math.min(self.buffer_rate + load1,rate)
                self.life:set_width(self.max_w*self.buffer_rate)
            elseif rate<self.buffer_rate then
                self.buffer_rate = math.max(self.buffer_rate - load1,rate)
                self.life:set_width(self.max_w* rate)
            end
            if unit.hide_buffer==nil and self.buffer_rate~=rate then
                local start = math.min(rate,self.buffer_rate)
                local size = math.abs(rate - self.buffer_rate)
                self.buffer:set_position(self.max_w* start,self.buffer.y)
                self.buffer:set_width(self.max_w* size)
                self.buffer:show()
            else
                self.buffer:hide()
            end

            if unit.level_class then
                self.rank:set_text(unit.level_class)
            else
                self.rank:set_text(unit.level)
            end
            if self.updata_buff then
                self:updata_buff(unit)
            end
            if self.updata_cost then
                self:updata_cost(unit)
            end
            if self.updata_shield then
                self:updata_shield(unit)
            end
            local cost_path = unit:get_data('能量贴图')
            if player==unit.owner then
                self.life:set_normal_image([[core\hpbar\绿条.tga]])
                self.cost:set_normal_image(cost_path or [[core\hpbar\蓝条.tga]])
            elseif player:is_ally(unit) then
                self.life:set_normal_image([[core\hpbar\蓝条.tga]])
                self.cost:set_normal_image(cost_path or [[core\hpbar\蓝条.tga]])
            elseif player:is_enemy(unit) and unit.owner~=ac.player[16] then
                self.life:set_normal_image([[core\hpbar\红条.tga]])
                self.cost:set_normal_image(cost_path or [[core\hpbar\红条.tga]])
            else
                self.life:set_normal_image([[core\hpbar\黄条.tga]])
            end
        end,
    }

    local rank = panel:add_panel([[core\hpbar\backdrop-level.tga]],-25,-1,25,27)

    panel.rank = rank:add_text('21',-1,2,25,25,10,5)
    panel.title = panel:add_text('筑基中期 手残玩家',-2,-15,w,10,10,5)
    panel.state = panel:add_panel([[core\hpbar\黄条.tga]],1,-3,w-5,3)
    panel.life = panel:add_panel([[core\hpbar\绿条.tga]],1,3,w-5,11)
    panel.cost = panel:add_panel([[core\hpbar\蓝条.tga]],1,17,w-5,3)

    panel.shield = panel.life:add_panel([[core\hpbar\ShieldBar.tga]])
    panel.shield:set_alpha(0.9)
    panel.shield:hide()

    panel.buffer = panel.life:add_panel([[core\hpbar\ShieldBar.tga]])
    panel.buffer:set_alpha(0.5)

    panel.life:add_panel([[core\hpbar\Line.tga]])
    panel.max_w = panel.life.w
    panel:hide()

    return panel
end

local w,h = 130,25
--重复利用组
local group1 = {}
local remove1 = {}

function unit_add_life(unit)
    local panel = table.remove(remove1,1)
    if not panel then
        panel = create_life1(w,h)
    end
    panel.buffer_rate = 1

    panel:bind_world(unit)
    japi.SetUnitPressUIVisible(unit.handle,false)
    group1[unit.handle] = panel

    unit:event '单位-即将移除'(function()
        group1[unit.handle] = nil
        table.insert(remove1,panel)
    end)
    return panel
end


ac.hardware:on "界面绘制" (function ()
    for handle,panel in pairs(group1) do
        if panel.show_flag then
            panel:update()
        end
    end

end)
